package com.maben.vue_support.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 密码工具类
 * 使用的事security中的bCryptPasswordEncoder编码
 */
@Component
public class PasswordUtil {
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    private static PasswordUtil passwordUtil;

    @PostConstruct
    public void init() {
        passwordUtil = this;
        passwordUtil.bCryptPasswordEncoder = this.bCryptPasswordEncoder;
    }

    /**
     * 明文密码加密
     */
    public static String encrypt(String password) {
        return passwordUtil.bCryptPasswordEncoder.encode(password);
    }

    /**
     * 密码验证
     */
    public static Boolean verify(String password, String passwordEncrypt) {
        return passwordUtil.bCryptPasswordEncoder.matches(password, passwordEncrypt);
    }
}
